package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.web.dao.IUserDao;
import com.situ.web.entity.Student;
import com.situ.web.entity.User;
import com.situ.web.util.JDBCUtil;

public class UserDaoImpl implements IUserDao {

	@Override
	public User login(String userName, String userPassword) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		User user = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name`,`password` from user Where name =? and password =?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1,userName);
			preparedStatement.setString(2,userPassword);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				String password = resultSet.getString("password");
				user = new User(id, name, password);
				System.out.println(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return user;
	}

	@Override
	public List<User> selectByName(String searchName) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<User> list = new ArrayList<User>();
		System.out.println("UserDaoImpl.selectByName()");
		System.out.println(searchName);
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select id,`name`,`password` from user  Where name = ? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, searchName);
			
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				String password = resultSet.getString("password");
				User user = new User(id, name, password);
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public int insert(User user) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		System.out.println("UserDaoImpl.insert()");
		System.out.println(user.getName());
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO user(`name`,`password`) VALUES(?,?)";
			System.out.println(sql);
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, user.getName());
			preparedStatement.setString(2, user.getPassword());
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return count;
	}

}
